package dr.server.impl.jpush;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonParser;
import dr.common.ResultModel;
import dr.common.utils.JpushClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JPusherService {

    static final String tag = "ZSKJ";

    @Autowired
    JpushClientUtil jpushClientUtil;

    public ResultModel push(String msgType, String title, String to, String messageOrTemplate, String time, String param) {
        try {
            int isOK = 0;
            //需要定义场景
            switch (msgType) {
                case "1":
                    isOK = jpushClientUtil.sendToRegistrationId(CollectionUtil.toList(to), title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject());
                    break;
                case "2":
                    isOK = jpushClientUtil.sendToRegistrationId2(CollectionUtil.toList(to), title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject(), time);
                    break;
                case "3":
                    isOK = jpushClientUtil.sendToAll(tag, title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject());
                    break;
                case "4":
                    isOK = jpushClientUtil.sendToAllTiming(tag, title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject(), time);
                    break;
                case "5":
                    isOK = jpushClientUtil.sendToAllAndroid(tag, title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject());
                    break;
                case "6":
                    isOK = jpushClientUtil.sendToAllIos(tag, title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject());
                    break;
                case "7":
                    //推送给 指定用户 list
                    isOK = jpushClientUtil.sendToRegistrationId(JSONObject.parseArray(to, String.class), title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject());
                    break;
                case "8":
                    //推送给 指定用户 list(定时)
                    isOK = jpushClientUtil.sendToRegistrationId2(JSONObject.parseArray(to, String.class), title, title, messageOrTemplate, new JsonParser().parse(param).getAsJsonObject(),time);
                    break;
            }

            if(isOK == 1) {
                return ResultModel.ok();
            }

            return ResultModel.err();
        }catch (Exception e) {
            return ResultModel.fail(e.getLocalizedMessage());
        }
    }
}
